package com.cms.intelligent_control.module

import android.app.Activity
import android.view.View
import android.widget.ImageView
import android.widget.TextView
import com.bumptech.glide.Glide
import com.cms.intelligent_control.R
import com.cms.intelligent_control.common.TTSHelper
import org.json.JSONObject

class CommonModule(var activity: Activity) {
    private var mVoiceTextView = activity.findViewById<TextView>(R.id.voice_text)
    private var mElfishImageView = activity.findViewById<ImageView>(R.id.iv_elfish)
    private val mTTSHelper by lazy {
        TTSHelper()
    }

    private var mActiveStatus = 1 //1、活跃的 2、休眠的

    public fun isActive(): Boolean {
        return mActiveStatus == 1
    }

    public fun isCommonAction(data: JSONObject): Boolean {
        val type = data.optString("instruct_type")
        return arrayOf("隐藏显示", "唤醒休眠").contains(type)
    }

    public fun execute(data: JSONObject) {
        val type = data.optString("instruct_type")
        val name = data.optString("instruct_name")
        val operation = data.optString("exe_operation")
        if (type == "唤醒休眠") {
            if (name == "你好") {
                mActiveStatus = 1
                Glide.with(mElfishImageView).load(R.mipmap.elfish_active).into(mElfishImageView)
            } else if (name == "休眠" || name == "再见") {
                mActiveStatus = 2
                mVoiceTextView.text = ""
                Glide.with(mElfishImageView).load(R.mipmap.elfish_disable).into(mElfishImageView)
            }
        } else if (type == "隐藏显示") {
            when (name) {
                "小精灵隐藏" -> mElfishImageView.visibility = View.INVISIBLE
                "小精灵显示" -> mElfishImageView.visibility = View.VISIBLE
                "文字隐藏" -> mVoiceTextView.visibility = View.INVISIBLE
                "文字显示" -> mVoiceTextView.visibility = View.VISIBLE
            }
        }
    }


    public fun startTts(obj: JSONObject) {
        val answer = obj.optString("answer")
        if (answer.isNotBlank()) {
            mTTSHelper.startTTS(answer)
        }
    }
}